//
//  ViewController.m
//  CoreData_81
//
//  Created by Chris on 15/11/20.
//  Copyright © 2015年 Chris. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"

@interface ViewController ()

// 创建一个数据管理属性
@property (nonatomic, strong) CoreDataManager *manager;

- (IBAction)add:(id)sender;
- (IBAction)select:(id)sender;
- (IBAction)update:(id)sender;
- (IBAction)delete:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.manager = [CoreDataManager defaults];
    
    NSLog(@"address:  %@", [self.manager applicationDocumentsDirectory]);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark   -   add增加
- (IBAction)add:(id)sender {
    // 创建一个实体描述类
    // 参数1：描述的实体（比如这个里边用到的Student，首字母大写）
    // 参数2：数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    // 创建实体
    // 参数1：实体描述
    // 参数2：数据管理器
    Student *student = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    student.name = @"张三";
    student.age = @12;
    student.sex = @"男";
    student.hobby = @"吃好吃的";
    student.number = @20;
    
    [self.manager saveContext];
}

#pragma mark   -   select查找
- (IBAction)select:(id)sender {
    // 获取查询数据的请求，相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    // 添加一个约束谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"张三"];
    
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    NSLog(@"%ld", array.count);

}

#pragma mark   -   update修改
- (IBAction)update:(id)sender {
    // 获取查询数据的请求，相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    // 添加一个约束谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"张三"];
    
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    // 查询最后一个，然后修改了性别，再保存一下
    Student *stu = [array lastObject];
    stu.sex = @"女";
    [self.manager saveContext];
}

#pragma mark   -   delete删除
- (IBAction)delete:(id)sender {
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    [fetchRequest setEntity:entity];
    
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"女"];
    
    [fetchRequest setPredicate:predicate];
    
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sex"
                                                                   ascending:YES];
    
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    for (Student *stu in fetchedObjects) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    [self.manager saveContext];
}



@end
